Skip to content

feat: filesystem store backend#251

Merged
appleboy merged 11 commits intogin-contrib:masterfrom
geschke:master
May 23, 2025
Merged

feat: filesystem store backend#251
appleboy merged 11 commits intogin-contrib:masterfrom
geschke:master

Conversation

@geschke
Copy link
Copy Markdown
Contributor

@geschke geschke commented Nov 28, 2023

When testing the sessions middleware, I realized that a filesystem store was missing, which is the default in PHP. Upon examining the underlying Gorilla sessions library, I discovered an experimental implementation of a filesystem store. Despite its experimental status, it performed well in my tests when integrated into a Gin application using pure Gorilla sessions.

To support the Gorilla FilesystemStore, I added the necessary code in a filesystem backend module, provided by this pull request.

While testing the filesystem module, I noticed discrepancies in the test files for the backend modules. For instance, the cookie_test.go file didn't work as expected - the function "NewStore()" was missing. After modifying it to "cookie.NewStore()" and importing the "gin-contrib/sessions/cookie" module, the test ran successfully with the command "go test cookie/cookie_test.go". I didn't make changes to another module in this pull request, since I'm not very experienced with the Golang test system. Please correct me if I'm mistaken.

In the "filesystem_test.go" file, which is essentially a modified copy of the cookie test, I used the function "filesystem.NewStore()", and the tests ran successfully in my environment. However, the test revealed an issue with the Options settings in the FilesystemStore of the Gorilla sessions module version 1.2.1, used in this library. This problem has been resolved in the current version, 1.2.2, so I updated the version number in go.mod.

I would appreciate it if you could merge this pull request, as I believe the addition of the filesystem store is valuable, particularly for development and testing purposes.

Thanks in advance &
Kind regards,
Ralf

@manusa
Copy link
Copy Markdown

manusa commented Mar 5, 2024

I just came into this PR because I implemented the store based on gorilla/sessions FilesystemStore (identically 😳) in a downstream project and was looking to contribute it back upstream.

Is this PR (should fix #182) being considered?

@appleboy
Copy link
Copy Markdown
Member

@geschke Please help to take a look build fails.

@geschke
Copy link
Copy Markdown
Contributor Author

geschke commented Mar 25, 2024

Perhaps I misunderstood how the test file is meant to be applied. I attempted to run the tests locally, but encountered errors when using the commands specified in the testing.yml file. It appears that the issue lies in the import of the 'filesystem' package. Currently, the initial lines of filesystem_test.go are as follows

package filesystem

import (
	"os"
	"testing"

	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/filesystem"
	"github.com/gin-contrib/sessions/tester"
)

var sessionPath = os.TempDir()

var newStore = func(_ *testing.T) sessions.Store {
	store := filesystem.NewStore(sessionPath, []byte("secret"))
	return store
}
[...]

This works fine when I run go test filesystem/filesystem_test.go locally:

geschke@moabit:~/go/src/gin-contrib-sessions$ go test filesystem/filesystem_test.go
ok      command-line-arguments  0.025s

But it fails with the go test command in testing.yml.

When I remove the filesystem import and use the Newstore() function without the package name, the local test build fails. However, it's possible that it might work in the GitHub runner.

package filesystem

import (
	"os"
	"testing"

	"github.com/gin-contrib/sessions"
	//"github.com/gin-contrib/sessions/filesystem"
	"github.com/gin-contrib/sessions/tester"
)

var sessionPath = os.TempDir()

var newStore = func(_ *testing.T) sessions.Store {
	store := NewStore(sessionPath, []byte("secret"))
	return store
}

Please provide guidance on how to proceed. Should I commit the mentioned changes? Unfortunately, I seem unable to run the tests in my GitHub repository; perhaps I've overlooked something here.

Thanks in advance &
kind regards,
Ralf

@appleboy
Copy link
Copy Markdown
Member

Please resolve the conflicts.

@appleboy appleboy requested a review from Copilot May 23, 2025 07:09
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request adds support for a filesystem store backend using the experimental Gorilla sessions FilesystemStore to enable session persistence for Gin applications.

  • Introduces a new filesystem store module with its own NewStore and Options API.
  • Provides comprehensive tests for session get/set, delete, flashes, clear, options, and multi-session handling.
  • Updates the sample usage in the _example directory and enriches the README documentation with filesystem instructions.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
filesystem/filesystem_test.go New tests for verifying the filesystem session functionality
filesystem/filesystem.go New implementation wrapping Gorilla's FilesystemStore
_example/filesystem/main.go Example code demonstrating how to use the filesystem backend
README.md Documentation updated to include filesystem store usage

@codecov-commenter
Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.36%. Comparing base (5c9a349) to head (728dc67).
Report is 29 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff             @@
##           master     #251       +/-   ##
===========================================
+ Coverage   64.00%   82.36%   +18.36%     
===========================================
  Files           8       13        +5     
  Lines          75      431      +356     
===========================================
+ Hits           48      355      +307     
- Misses         25       59       +34     
- Partials        2       17       +15     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@appleboy appleboy changed the title Add filesystem store backend feat: filesystem store backend May 23, 2025
@appleboy appleboy merged commit 10bdc64 into gin-contrib:master May 23, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants